#pragma once
#include<vector>
#include<functional>

namespace bit
{
    template<class T>
    struct less
    {
        bool operator()(const T& left, const T& right)
        {
            return left < right;
        }
    };

    template<class T>
    struct greater
    {
        bool operator()(const T& left, const T& right)
        {
            return left > right;
        }
    };

    template <class T, class Container = vector<T>, class Compare = less<T> >
    class priority_queue
    {
    public:
        priority_queue() //= default;
            :c()
        {}

        template <class InputIterator>
        priority_queue(InputIterator first, InputIterator last)
            //:c(first,last) //和下面的一样，都是将数据输入c中
        {
            while (first != last)
            {
                c.push_back(*first);
                ++first;
            }

            for (int i = c.size() / 2 - 1; i >= 0; i--) {
                AdjustDown(i);
            }
        }

        bool empty() const
        {
            return c.empty();
        }

        size_t size() const
        {
            return c.size();
        }

        const T& top() const
        {
            return c[0]; //上下一样的
            //return c.front();
        }

        void AdjustUp(int child)
        {
            int parent = (child - 1) >> 1;
            while (child > 0)
            {
                if (comp(c[parent], c[child]))
                {
                    std::swap(c[parent], c[child]);
                    child = parent;
                    parent = (child - 1) >> 1;
                }
                else break;
            }
        }

        void AdjustDown(int parent)
        {
            int child = parent * 2 + 1;
            while (child < c.size())
            {
                if (child + 1 < c.size() && comp(c[child], c[child + 1]))
                { 
                    ++child;
                }
                if (comp(c[parent], c[child]))
                {
                    std::swap(c[parent], c[child]);
                    parent = child;
                    child = parent * 2 + 1;
                }
                else break;
            }
        }


        void push(const T& x)
        {
            c.push_back(x);
            AdjustUp(c.size() - 1);
        }

        void pop()
        {
            if (empty()) return;
            std::swap(c.front(), c.back());
            c.pop_back();
            AdjustDown(0);
        }

    private:
        Container c;
        Compare comp;
    };

};